package com.xzlf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xzlf.domain.WebSite;
import com.xzlf.service.WebSiteService;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xzlf.mapper.NovelChapterMapper;
import com.xzlf.domain.NovelChapter;
import com.xzlf.service.NovelChapterService;

@Service
public class NovelChapterServiceImpl extends ServiceImpl<NovelChapterMapper, NovelChapter> implements NovelChapterService {

    private static List<NovelChapter> list = new ArrayList<>();
    @Autowired
    private NovelChapterMapper novelChapterMapper;
    @Autowired
    private WebSiteService webSiteService;

    @Override
    public boolean saveNovel(String pid, String url, int popNum) {
        Document document = null;
        try {
            document = Jsoup.connect(url).get();

            Document parse = Jsoup.parse(document.html());
//            Elements elements = parse.select("#list > dl > dd > a");
            Elements elements = parse.select("body > div.listmain > dl > dd > a");
            System.out.println(elements.size());
            for (Element element : elements) {
                NovelChapter novelChapter = new NovelChapter();
                novelChapter.setPid(Integer.parseInt(pid));
                novelChapter.setTitle(element.text().trim());
                novelChapter.setUri(element.attr("href").trim());
                list.add(novelChapter);
            }
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }

        for (int i = 0; i < popNum; i++) {
            list.remove(0);
        }
        return novelChapterMapper.saveBatch(list);
    }

    @Override
    public List<NovelChapter> findByPage(int current, int pageSize, int pid) {
        Page<NovelChapter> page = page(new Page<NovelChapter>(current, pageSize),
                new LambdaQueryWrapper<NovelChapter>()
                        .eq(NovelChapter::getPid, pid)
                        .orderByAsc(NovelChapter::getId));
        List<NovelChapter> records = page.getRecords();
        for (NovelChapter record : records) {
            WebSite webSite = webSiteService.getById(pid);
            record.setContentUrl(webSite.getUrl() + record.getUri());
        }
        return records;
    }

    @Override
    public String getContent(String url) {
        Document document = null;
        try {
            document = Jsoup.connect(url).get();
            Document parse = Jsoup.parse(document.html());
            Elements elements = parse.select("#content");
            System.out.println(elements.size());
            String text = elements.get(0).html();
            System.out.println(text);
            return text;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}
